import axios from 'axios';
import router from '@/router/index'
import authorization from '@/api/author'
import {
  MessageBox,
  Message
} from 'element-ui'
let CancelToken = axios.cancelToken
axios.defaults.timeout = 20000;//超时报错
axios.defaults.baseURL = process.env.BASEURL; //配置url
var AxiosUrl = 'http://192.168.217.71:9001'
// var AxiosUrl = "http://103.10.87.51:8092"
let pending = [];
let cancelToken = axios.CancelToken; // `cancelToken` 指定用于取消请求的 cancel token
let cancelPending = (config) => {
  pending.forEach((item, index) => {
    if (!!config) {
      if (item.u == config.url) {
        item.f(); //取消请求
        pending.splice(index, 1); //移除当前请求记录
      };
    } else {
      item.f(); //取消请求
      pending.splice(index, 1); //移除当前请求记录
    }
  });
};
//验证登录状态
router.beforeEach((to, from, next) => {
  //   axios({
  //     method: 'get',
  //     url: AxiosUrl+'/sys/menu/phoneNav',
  //     headers: {
  //         'Content-type': 'application/json'
  //     },
  // })
  //     .then((response) => {
  //         console.log(response);
  //     })
  //     .catch((error) => {
  //         console.log(error);
  //     }
  // );
  if (to.matched.some(record => record.meta.requireAuth)) {
    let token = JSON.parse(authorization()).token;
    //如果已经登录 则正常进入
    if (!!token) {
      cancelPending();
      next();

    } else {
      next({
        'name': 'login',
        'query': {
          'redirect': to.fullPath
        }
      });
    }
  } else if (to.name == 'login') { //如果是登录页，则验证如果当前是登录状态自动跳转至系统主页，否则正常进入登录页
    let token = JSON.parse(authorization()).token;
    //如果已经登录，则重定向至系统首页
    if (!!token) {
      router.push({
        'name': 'Main'
      });
    } else {
      next();
    };
  } else { //其他页面正常进入
    next();
  };

})

//拦截器
axios.interceptors.request.use(
  config => {
    let token = JSON.parse(authorization()).token;
    if (!!token) {
      config.headers = {
        'Content-Type': 'application/json',
        'token': token,
      }
    } else {
      config.headers = {
        'Content-Type': 'application/json',
      }
    }
    cancelPending(config);
    config.CancelToken = new cancelToken((c) => {
      pending.push({
        'u': config.url,
        'f': c
      })
    })
    config.data = JSON.stringify(config.data);
    return config;
  }

)
/**
 * 封装get方法
 * @param url
 * @param data
 * @returns {Promise}
 */

export function fetch(url, data = {}) {
  return new Promise((resolve, reject) => {
    axios.get(AxiosUrl + url).then(response => {
      if (response && response.data) {
        resolve(response.data);
      }
    })
      .catch(err => {
        reject(err)
      })
  })
}

/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */
var Promise = require('bluebird');
export function post(url, data = {}) {
  return new Promise((resolve, reject) => {
    axios.post(AxiosUrl + url, data)
      .then(response => {
        if (response && response.data) {
          resolve(response.data);
        }
      })
      .catch(err => {
        reject(err)
      })
  })
}
